﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class HoaDon : Form
    {
        string ngayDT;
        string MSHD;
        public HoaDon()
        {
            InitializeComponent();
        }

        private void HoaDon_Load(object sender, EventArgs e)
        {
            comboBoxLoadMSKH();
            comboBoxMSNV();
            LoadDataGridView();
            
        }

        private void LoadDataGridView()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "Select MaSo_HD,NgayDatHang,XacNhan,HoTen_KH,HoTen_NV from HoaDon join KhachHang on HoaDon.MaSo_KH=KhachHang.MaSo_KH join NhanVien on HoaDon.MaSo_NV=NhanVien.MaSo_NV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "HoaDon");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["HoaDon"];
            conn.Close();
        }

        private void LoadDataGridViewHDCT(string MSHD )
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select HoaDon_CT.MaSo_HD ,HoaDon_CT.MaSo_SP ,Ten_SP,SoLuong from HoaDon_CT join SanPham on HoaDon_CT.MaSo_SP=SanPham.MaSo_SP where HoaDon_CT.MaSo_HD=@MaSo_HD ";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@MaSo_HD", MSHD);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "HoaDon_CT");
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = ds.Tables["HoaDon_CT"];
            conn.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            MSHD = dataGridView1.SelectedRows[0].Cells["MaSo_HD"].Value.ToString();
            LoadDataGridViewHDCT(MSHD);
        }
       

        private void comboBoxLoadMSKH()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from KhachHang";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "KhachHang");
            DataTable dt = ds.Tables["KhachHang"];
            comMSKH.DisplayMember = "HoTen_KH";
            comMSKH.ValueMember = "MaSo_KH";
            comMSKH.DataSource = ds.Tables["KhachHang"].DefaultView;
            conn.Close();
        }
        private void comboBoxMSNV()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from NhanVien";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "NhanVien");
            DataTable dt = ds.Tables["NhanVien"];
            comMSNV.DisplayMember = "HoTen_NV";
            comMSNV.ValueMember = "MaSo_NV";
            comMSNV.DataSource = ds.Tables["NhanVien"].DefaultView;
            conn.Close();
        }

        private void buttonExitHD_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
       

        //private void buttonAddHD_Click(object sender, EventArgs e)
        //{
        //    ngayDT = comThangDT.Text + "/" + comNgayDT.Text + "/" + comNamDT.Text;
        //    themHD();
        //    LoadDataGridView();
        //}
        private void themHD()
        {
            if (txtMSHD.Text.Length == 0 || comNgayDT.Text.Length == 0 || comThangDT.Text.Length == 0 || comNamDT.Text.Length == 0)
                TBTCongHD.Text = "Thêm mới hóa đơn không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "insert into HoaDon(MaSo_HD,NgayDatHang,XacNhan,MaSo_KH,MaSo_NV) values (@MaSo_HD,@NgayDatHang,@XacNhan,@MaSo_KH,@MaSo_NV)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_HD", txtMSHD.Text);
                cmd.Parameters.AddWithValue("@NgayDatHang", ngayDT);
                cmd.Parameters.AddWithValue("@MaSo_KH", comMSKH.SelectedValue);
                cmd.Parameters.AddWithValue("@MaSo_NV", comMSNV.SelectedValue);
                if (radioTrue.Checked == true)
                {
                    cmd.Parameters.AddWithValue("@XacNhan", true);
                }
                else
                {
                    cmd.Parameters.AddWithValue("@XacNhan", false);
                }
                cmd.ExecuteNonQuery();
                TBTCongHD.Text = "Thêm mới hóa đơn thành công ";
                conn.Close();
            }
        }

        private void buttonUpdateHD_Click(object sender, EventArgs e)
        {
            ngayDT = comThangDT.Text + "/" + comNgayDT.Text + "/" + comNamDT.Text;
            CapNhatHD();
            LoadDataGridView();
        }
        private void CapNhatHD()
        {
            if (txtMSHD.Text.Length == 0 || comNgayDT.Text.Length == 0 || comThangDT.Text.Length == 0 || comNamDT.Text.Length == 0)
                TBTCongHD.Text = "Cập nhật hóa đơn không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "update HoaDon set NgayDatHang=@NgayDatHang,XacNhan=@XacNhan,MaSo_KH=@MaSo_KH,MaSo_NV=@MaSo_NV where MaSo_HD=@MaSo_HD";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_HD", txtMSHD.Text);
                cmd.Parameters.AddWithValue("@NgayDatHang", ngayDT);
                cmd.Parameters.AddWithValue("@MaSo_KH", comMSKH.SelectedValue);
                cmd.Parameters.AddWithValue("@MaSo_NV", comMSNV.SelectedValue);
                
                if (radioTrue.Checked == true)
                {
                    cmd.Parameters.AddWithValue("@XacNhan", true);
                }
                else
                {
                    cmd.Parameters.AddWithValue("@XacNhan", false);
                }
                cmd.ExecuteNonQuery();
                TBTCongHD.Text = "Cập nhật hóa đơn thành công ";
                conn.Close();
            }
        }

        //private void buttonDelHD_Click(object sender, EventArgs e)
        //{
        //    DeleteHD();
        //    LoadDataGridView();
        //}
        //private void DeleteHD()
        //{
        //    if (txtMSHD.Text.Length == 0 || comNgayDT.Text.Length == 0 || comThangDT.Text.Length == 0 || comNamDT.Text.Length == 0)
        //        TBTCongHD.Text = "Xóa hóa đơn không thành công";
        //    else
        //    {
        //        Config file = new Config(Application.StartupPath + "\\Config.ini");
        //        string connectionstring = file.ReadValue("server", "con");
        //        SqlConnection conn = new SqlConnection(connectionstring);
        //        string sql = "delete from HoaDon where MaSo_HD = @MaSo_HD";
        //        SqlCommand cmd = new SqlCommand(sql, conn);
        //        conn.Open();
        //        cmd.Parameters.AddWithValue("@MaSo_HD", txtMSHD.Text);
        //        cmd.ExecuteNonQuery();
        //        conn.Close();
        //        TBTCongHD.Text = "Xóa loại sản phẩm thành công";
        //    }
        //}

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            MSHD =dataGridView1.Rows[e.RowIndex].Cells["MaSo_HD"].Value.ToString();
            LoadHD(MSHD);
        }
        private void LoadHD(string MSHD)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * ,convert(char(10),NgayDatHang,103) as ngaydathang from HoaDon where MaSo_HD = @MaSo_HD";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_HD", MSHD);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
               txtMSHD.Text = reader["MaSo_HD"].ToString();
               comNgayDT.Text= ((string)reader["ngaydathang"]).Split('/')[0];
               comThangDT.Text = ((string)reader["ngaydathang"]).Split('/')[1];
               comNamDT.Text = ((string)reader["ngaydathang"]).Split('/')[2];
               if ((bool)reader["XacNhan"] == true)
                   radioTrue.Checked = true;
               else
                   radioFalse.Checked = true;
               comMSKH.SelectedValue = reader["MaSo_KH"].ToString();
               comMSNV.SelectedValue = reader["MaSo_NV"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        //private void buttonClearHD_Click(object sender, EventArgs e)
        //{
        //    txtMSHD.Text = null;
        //    comNgayDT.Text = null;
        //    comThangDT.Text = null;
        //    comNamDT.Text = null;
        //    TBTCongHD.Text = null;
        //}

        

       
       
    }
}
